package homework13;

import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class home0003 {
    public static void main(String[] args) throws IOException {
        FileInputStream is = new FileInputStream("决策引擎Java-data.txt");
        String resultbe = IOUtils.toString(is, String.valueOf(StandardCharsets.UTF_8));
        String result = replaceBlank(resultbe);
        String[] result1 = result.split("：");
        String result2 = result1[1].replace("30分钟", "");
        String result3 = result1[2].replace("15分钟", "");
        Map<String, String> map1 = tomap(result2, String.valueOf(result1[3]));
        List<Map.Entry<String, String>> list = new ArrayList<>(map1.entrySet());
        Collections.sort(list, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println("<"+list.get(i).getKey() + ", " + list.get(i).getValue()+">");
//        }
        list.forEach(m->System.out.println("<"+m.getKey() + ", " + m.getValue()+">"));
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }


    private static Map<String, String> tomap(String s, String s2) {
        Map<String, String> map = new HashMap<>();
        String[] strings = s.split(";");
        String[] strings2 = s2.split(";");
        int num = 4;
        S:
        for (int i = 0; i < strings.length; i++) {
            for (int j = 0; j < strings2.length; j++) {
                String[] strings3 = strings2[j].split(",");
                for (int k = 0; k < strings3.length; k++) {
                    String[] strings4 = strings3[k].split(":");
                    String[] strings5 = strings4[0].split("~|-");
                    if (num != 0) {
                        StringBuilder sb = new StringBuilder();
                        sb.append(String.valueOf(0).concat(strings[i]).concat(strings5[1]));
//                        System.out.println(sb);
                        map.put(sb.toString(), strings4[1]);
                        num--;
                    } else if (num == 0) {
                        num = 4;
                        continue S;
                    }
                }
            }
        }
        return map;
    }
}
